<template>
    <div class="EditRewardPopup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
				<el-form-item label="奖励名称" prop="Name">
				    <el-input v-model="formData.Name" placeholder="请输入奖励名称" clearable />
				</el-form-item>
				<el-form-item label="奖励类别" prop="Category">
                    <el-input v-model="formData.Category" placeholder="请输入奖励类别" clearable />
                </el-form-item>
				<el-form-item label="奖励代码" prop="Code">
                    <el-input v-model="formData.Code" placeholder="请输入奖励代码" clearable />
                </el-form-item>
				<el-form-item label="奖励级别" prop="Grade">
                    <el-input v-model="formData.Grade" placeholder="奖励级别" clearable />
                </el-form-item>
				<el-form-item label="批准日期" prop="PassDate">
                    <el-date-picker v-model="formData.PassDate" type="date" />
                </el-form-item>
				<el-form-item label="机关名称" prop="OrganizationName">
                    <el-input v-model="formData.OrganizationName" placeholder="请输入批准机关名称" clearable />
                </el-form-item>
				<el-form-item label="批准机关代码" prop="	OrganizationCode">
                    <el-input v-model="formData.OrganizationCode" placeholder="批准机关代码" clearable />
                </el-form-item>
				<el-form-item label="批准机关类别" prop="	CategoryOfOrganization">
                    <el-input v-model="formData.CategoryOfOrganization" placeholder="批准机关类别" clearable />
                </el-form-item>
				<el-form-item label="批准机关层次" prop="	OrganizationGrade">
                    <el-input v-model="formData.OrganizationGrade" placeholder="批准机关层次" clearable />
                </el-form-item>
				<el-form-item label="批准机关级别" prop="	OrganizationLevel">
                    <el-input v-model="formData.OrganizationLevel" placeholder="批准机关级别" clearable />
                </el-form-item>
				<el-form-item label="奖励原因" prop="Reason">
				    <el-input v-model="formData.Reason" placeholder="批准奖励原因" clearable />
				</el-form-item>
				<el-form-item label="批准文号" prop="OrderNumber">
				    <el-input v-model="formData.OrderNumber" placeholder="批准文号" clearable />
				</el-form-item>
				<el-form-item label="文件名称" prop="DocumentName">
				    <el-input v-model="formData.DocumentName" placeholder="文件名称" clearable />
				</el-form-item>
				<el-form-item label="奖励变化" prop="DocumentNameOfChange">
				    <el-input v-model="formData.DocumentNameOfChange" placeholder="奖励变化" clearable />
				</el-form-item>
				<el-form-item label="奖励变化类别" prop="CategoryOfChange">
				    <el-input v-model="formData.CategoryOfChange" placeholder="奖励变化类别" clearable />
				</el-form-item>
				<el-form-item label="奖励变化原因" prop="ReasonOfChange">
				    <el-input v-model="formData.ReasonOfChange" placeholder="奖励变化原因" clearable />
				</el-form-item>
				<el-form-item label="奖励变化时间" prop="ChangedDate">
                    <el-date-picker v-model="formData.ChangedDate" type="date" />
                </el-form-item>
				<el-form-item label="受奖励时职务" prop="	RewardingPostion">
				    <el-input v-model="formData.RewardingPostion" placeholder="受奖励时职务" clearable />
				</el-form-item>
				<el-form-item label="受奖励职务层次" prop="RewardingPostionGrade">
				    <el-input v-model="formData.RewardingPostionGrade" placeholder="受奖励职务层次" clearable />
				</el-form-item>
				<el-form-item label="荣誉称号" prop="HonorName">
                    <el-input v-model="formData.HonorName" clearable />
                </el-form-item>
				<el-form-item label="追授标志" prop="AppendRank">
                    <el-input v-model="formData.AppendRank" clearable />
                </el-form-item>
				<el-form-item label="事迹材料" prop="Description">
				    <el-input v-model="formData.Description" clearable />
				</el-form-item>
				<el-form-item label="奖励说明" prop="Remark">
				    <el-input v-model="formData.Remark" clearable />
				</el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="EditRewardPopup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'

import { EditReward} from '@/api/consumer'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑奖励信息' : '添加奖励信息'
})
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
    id: 0,
    member_id: props.member_id,
     Name: '',
     Category: '',
     Code: '',
     Grade: '',
     PassDate: '',
     OrganizationName: '',
     OrganizationCode: '',
     CategoryOfOrganization: '',
	 OrganizationGrade: '',
	 OrganizationLevel: '',
	 Reason: '',
	 OrderNumber: '',
	 DocumentName: '',
	 DocumentNameOfChange: '',
	 CategoryOfChange: '',
	 ReasonOfChange: '',
	 ChangedDate: '',
	 RewardingPostion: '',
	 RewardingPostionGrade: '',
	 HonorName: '',
	 Description: '',
	 Remark: ''
})

const formRules = reactive({
     Name: [
         {
             required: true,
             message: '请输入奖励名称',
             trigger: ['blur']
         }
     ],
     PassDate: [
         {
             required: true,
             message: '请输入批准日期',
             trigger: ['blur']
        }
     ],
     OrganizationName: [
         {
             required: true,
             message: '请输入批准机关名称',
             trigger: ['blur']
         }
     ]
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('学历表单内容', formData)
    await EditReward(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formData.id = row.id
	const data = await EditReward({
	    id: row.id,
			 mode:'get'
	})
	for (const key in formData) {
	    if (data[key] != null && data[key] != undefined) {
	        formData[key] = data[key]
	    }
	}
   
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
